package indi.yangshenhui.spring.controller;

import indi.yangshenhui.hbase.dao.HBaseHelper;
import indi.yangshenhui.hbase.dao.constant.HBaseConstant;
import indi.yangshenhui.hbase.dao.entity.htable.HBaseTableDescribesEntity;
import indi.yangshenhui.hbase.dao.entity.htable.HBaseTableFamilyDescribesEntity;
import indi.yangshenhui.hbase.dao.pool.HTablePoolEngine;
import indi.yangshenhui.spring.constant.PropertiesConstant;
import indi.yangshenhui.tool.StringUtil;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.hbase.io.hfile.Compression.Algorithm;
import org.apache.hadoop.hbase.regionserver.StoreFile.BloomType;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class CreateTableController {
	private static final Logger LOGGER = Logger
			.getLogger(CreateTableController.class);

	@RequestMapping("createtable.do")
	public String createTable(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "tablename") String tableName,
			@RequestParam(value = "familyname") String familyName,
			@RequestParam(value = "maxfilesize") String maxFileSize,
			@RequestParam(value = "memstoreflushsize") String memstoreFlushSize,
			@RequestParam(value = "prebuiltone") String preBuiltOne,
			@RequestParam(value = "prebuilttwo") String preBuiltTwo,
			@RequestParam(value = "isreadonly") String isReadOnly,
			@RequestParam(value = "isdeferredlogflush") String isDeferredLogFlush,
			@RequestParam(value = "timetolive") String timeToLive,
			@RequestParam(value = "bloomtype") String bloomType,
			@RequestParam(value = "compression") String compression,
			@RequestParam(value = "compactioncompression") String compactionCompression,
			@RequestParam(value = "maxversions") String maxVersions,
			@RequestParam(value = "inmemory") String inMemory,
			@RequestParam(value = "blocksize") String blockSize,
			@RequestParam(value = "isblockcacheenabled") String isBlockCacheEnabled) {
		request.setAttribute("message", "create table");
		if (StringUtil.isEmpty(tableName) || tableName.startsWith("Please")) {
			return "failure";
		}
		if (StringUtil.isEmpty(familyName) || familyName.startsWith("Please")) {
			return "failure";
		}
		String[] families = null;
		if (familyName.contains(",")) {
			families = familyName.split(",");
		} else {
			families = new String[] { familyName };
		}
		HBaseTableDescribesEntity hbDescribesEntity = new HBaseTableDescribesEntity();
		hbDescribesEntity.setTableName(tableName);
		Map<String, HBaseTableFamilyDescribesEntity> familyMap = new HashMap<String, HBaseTableFamilyDescribesEntity>();
		try {
			hbDescribesEntity.setMaxFileSize(Long.parseLong(maxFileSize));
		} catch (Exception e) {
			hbDescribesEntity.setMaxFileSize(0);
			LOGGER.error(e.getMessage(), e);
		}
		try {
			hbDescribesEntity.setMemstoreFlushSize(Long
					.parseLong(memstoreFlushSize));
		} catch (Exception e) {
			hbDescribesEntity.setMemstoreFlushSize(0);
			LOGGER.error(e.getMessage(), e);
		}
		if (StringUtil.isNotEmpty(preBuiltOne)) {
			if (!preBuiltOne.startsWith("Please")) {
				String[] preBuilts = preBuiltOne.split(",");
				if (preBuilts.length >= 3) {
					// 指定开始和结束的rowKey以及预建region的数量
					hbDescribesEntity.setStartRowKey(preBuilts[0]);
					hbDescribesEntity.setEndRowKey(preBuilts[1]);
					try {
						hbDescribesEntity.setRegionNumber(Integer
								.parseInt(preBuilts[2]));
					} catch (Exception e) {
						hbDescribesEntity.setStartRowKey(null);
						hbDescribesEntity.setEndRowKey(null);
						hbDescribesEntity.setRegionNumber(0);
						LOGGER.error(e.getMessage(), e);
					}
				}
			}
		}
		if (StringUtil.isNotEmpty(preBuiltTwo)) {
			if (!preBuiltTwo.startsWith("Please")) {
				String[] preBuilts = preBuiltTwo.split(",");
				for (String rowKey : preBuilts) {
					// 指定切分的rowKey
					hbDescribesEntity.addSplitRowKey(rowKey);
				}
			}
		}
		try {
			hbDescribesEntity.setReadOnly(Boolean.parseBoolean(isReadOnly));
		} catch (Exception e) {
			hbDescribesEntity.setReadOnly(false);
			LOGGER.error(e.getMessage(), e);
		}
		try {
			hbDescribesEntity.setDeferredLogFlush(Boolean
					.parseBoolean(isDeferredLogFlush));
		} catch (Exception e) {
			hbDescribesEntity.setDeferredLogFlush(false);
			LOGGER.error(e.getMessage(), e);
		}
		for (String family : families) {
			HBaseTableFamilyDescribesEntity hBaseTableFamilyDescribesEntity = new HBaseTableFamilyDescribesEntity();
			try {
				hBaseTableFamilyDescribesEntity.setTimeToLive(Integer
						.parseInt(timeToLive));
			} catch (Exception e) {
				hBaseTableFamilyDescribesEntity.setTimeToLive(0);
				LOGGER.error(e.getMessage(), e);
			}
			if (bloomType.equals("ROW")) {
				hBaseTableFamilyDescribesEntity.setBloomType(BloomType.ROW);
			} else if (bloomType.equals("ROWCOL")) {
				hBaseTableFamilyDescribesEntity.setBloomType(BloomType.ROWCOL);
			}
			if (compression.equals("SNAPPY")) {
				hBaseTableFamilyDescribesEntity
						.setCompression(Algorithm.SNAPPY);
			} else if (compression.equals("LZO")) {
				hBaseTableFamilyDescribesEntity.setCompression(Algorithm.LZO);
			} else if (compression.equals("LZ4")) {
				hBaseTableFamilyDescribesEntity.setCompression(Algorithm.LZ4);
			} else if (compression.equals("GZ")) {
				hBaseTableFamilyDescribesEntity.setCompression(Algorithm.GZ);
			}
			if (compactionCompression.equals("SNAPPY")) {
				hBaseTableFamilyDescribesEntity
						.setCompactionCompression(Algorithm.SNAPPY);
			} else if (compactionCompression.equals("LZO")) {
				hBaseTableFamilyDescribesEntity
						.setCompactionCompression(Algorithm.LZO);
			} else if (compactionCompression.equals("LZ4")) {
				hBaseTableFamilyDescribesEntity
						.setCompactionCompression(Algorithm.LZ4);
			} else if (compactionCompression.equals("GZ")) {
				hBaseTableFamilyDescribesEntity
						.setCompactionCompression(Algorithm.GZ);
			}
			try {
				hBaseTableFamilyDescribesEntity.setMaxVersions(Integer
						.parseInt(maxVersions));
			} catch (Exception e) {
				hBaseTableFamilyDescribesEntity.setMaxVersions(0);
				LOGGER.error(e.getMessage(), e);
			}
			try {
				hBaseTableFamilyDescribesEntity.setInMemory(Boolean
						.parseBoolean(inMemory));
			} catch (Exception e) {
				hBaseTableFamilyDescribesEntity.setInMemory(false);
				LOGGER.error(e.getMessage(), e);
			}
			try {
				// 0表示不做修改
				hBaseTableFamilyDescribesEntity.setBlockSize(Integer
						.parseInt(blockSize));
			} catch (Exception e) {// 0表示不做修改
				hBaseTableFamilyDescribesEntity.setBlockSize(0);
				LOGGER.error(e.getMessage(), e);
			}
			try {
				hBaseTableFamilyDescribesEntity.setBlockCacheEnabled(Boolean
						.parseBoolean(isBlockCacheEnabled));
			} catch (Exception e) {
				hBaseTableFamilyDescribesEntity.setBlockCacheEnabled(true);
				LOGGER.error(e.getMessage(), e);
			}
			familyMap.put(family, hBaseTableFamilyDescribesEntity);
		}
		hbDescribesEntity.setFamilyMap(familyMap);
		boolean flag = false;

		System.out.println(hbDescribesEntity);

		try {
			HTablePoolEngine.createHBaseConfiguration(
					HBaseConstant.HBASE_DEFAULT_INSTANCE,
					PropertiesConstant.HBASE_PROPERTIES
							.getProperty("hbase.zookeeper.quorum"),
					PropertiesConstant.HBASE_PROPERTIES.getProperty(""
							+ "hbase.zookeeper.property.clientport"));
			flag = HBaseHelper.create(hbDescribesEntity,
					HBaseConstant.HBASE_DEFAULT_INSTANCE);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		if (flag) {
			return "success";
		} else {
			return "failure";
		}
	}

}